package com.hyh.toyshopping.controller;

import com.hyh.toyshopping.commons.CurUser;
import com.hyh.toyshopping.commons.Result;
import com.hyh.toyshopping.exceptions.PasswordException;
import com.hyh.toyshopping.exceptions.UsernameNotFoundException;
import com.hyh.toyshopping.model.User;
import com.hyh.toyshopping.model.param.LoginParam;
import com.hyh.toyshopping.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0
 * @BelongsProject:IntelliJ IDEA
 * @BelongsPackage:com.hyh.toyshopping.controller
 * @Author:hyhWTX
 * @ClassName:UserController
 * @CreateTime:2022年-12月-15日 12:54
 * @Description: TODO (一句话描述以下该类的功能)
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    /***
     * @Description: 用户登录
     * @param loginParam
     * @param session
     * @return Result
     *
     */

    @PostMapping("/login")
    //@RequestBody:请求体注解，它会自动将请求中的"payload"参数封装到指定的对象中
    public Result login(@RequestBody LoginParam loginParam, HttpSession session){
        try {
            try {
                //设置休眠时间
                TimeUnit.MICROSECONDS.sleep(1000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            CurUser curUser = userService.login(loginParam);
            if (curUser!=null){
                //将当前账户对象存入session中
                session.setAttribute("curUser",curUser);
            }
            //封装为一个Result对象并返回
            return Result.success("登陆成功！",curUser);
        }catch (UsernameNotFoundException e){
            e.printStackTrace();
            //生成一个Result对象并返回
            return Result.fail(500,e.getMessage());
        }catch (PasswordException e){
            e.printStackTrace();
            //生成一个Result对象并返回
            return Result.fail(500,e.getMessage());
        }
    }
    /***
     * @Description: 用户退出
     * @param session
     * @return Result
     */
    @DeleteMapping("/exit")
    public Result exit(HttpSession session){
        session.invalidate();
        return Result.success("退出成功！");
    }
    /***
     * @Description: 用户注册
     * @param user
     * @return Result
     */

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        userService.addUser(user);
        return Result.success("注册成功！");
    }
    @GetMapping("/getUserList")
    public List<User> getUserList(){
        return userService.getUserList();
    }

    @PutMapping("/updateStatus")
    public Result updateStatus(int user_status,int user_id){
        userService.updateStatus(user_id,user_status);
        String message = user_status==0?"解冻":"冻结";
        return Result.success(message+"成功");
    }
    @PostMapping("/changePassword")
    public Result  changePasswordByUserName(@RequestParam String username,@RequestParam String userpassword){
        userService.changePasswordByUserName(username,userpassword);
       return Result.success("修改成功！",123456);
    }
}
